﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BAL;
using VO;
using System.Data.SqlClient;
namespace BTL
{
    
    public partial class frmUser : Form
    {
        VO.LoginVO _loginVO;
        SqlConnection conn = new SqlConnection(Core.Connectionstring.Con.ToString());
        SqlDataAdapter da = new SqlDataAdapter();
        DataTable dt = new DataTable();
        SqlCommand cmd = new SqlCommand();
        public frmUser()
        {
            InitializeComponent();
        }
        
        private bool cheklogin(string ID, string password)
        {
            cmd.Parameters.Clear();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.StoredProcedure;
            conn.Open();
            cmd.CommandText = "user_xem";
            cmd.Parameters.Add("@ID", SqlDbType.NVarChar, 200).Value = ID.ToString();
            cmd.Parameters.Add("@Pwd", SqlDbType.NVarChar, 200).Value = password.ToString();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read() == true)
            {
                return true;
            }
            else
                return false;
            
        }
        private void metroTileDangnhap_Click(object sender, EventArgs e)
        {
            try
            {
                
                if (box_id != null)
                {
                    string ten = box_id.Text;
                    string pass = box_pwd.Text;
                    bool kq = cheklogin(ten, pass);
                    if (kq == true)
                    {
                        this.Hide();
                        frmMenu _cs = new frmMenu();
                        _cs.Show();
                    }
                    else
                        if (kq == false)
                        {
                            MessageBox.Show("LỖi ĐĂNG NHẬP", "LOI", MessageBoxButtons.OK);
                        }
                }

            }
            catch
            {
                MessageBox.Show("LỖi ĐĂNG NHẬP", "LOI", MessageBoxButtons.OK);
            }
        }

        private void metroTileDangky_Click(object sender, EventArgs e)
        {
            try
            {
                cmd.Connection = conn;
                cmd.CommandType = CommandType.StoredProcedure;
                conn.Open();
                cmd.CommandText = "user_them";
                cmd.Parameters.Add("@ID", SqlDbType.NVarChar, 200).Value = box_id.Text;
                cmd.Parameters.Add("@Pwd", SqlDbType.NVarChar, 200).Value = box_pwd.Text;
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                cmd.ExecuteNonQuery();
                MessageBox.Show("Them nguoi dung thanh cong!", "Thanh Cong!", MessageBoxButtons.OK);
                conn.Close();
            }
            catch
            {
                MessageBox.Show("Trung ten!", "Them nguoi dung loi!", MessageBoxButtons.OK);
            }
        }
        private void frmUser_Load(object sender, EventArgs e)
        {
            box_id.Text = "lethinh";
            box_pwd.Text = "thinh123";
        }

        private void data()
        {
            _loginVO = new LoginVO();
            _loginVO.ID = box_id.Text;
            _loginVO.Pwd = box_pwd.Text;
        }

        private void box_id_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.F1)
            {
                frmHelp _help = new frmHelp();
                _help.Show();
            }
        }
    }
}
